package com.dkd.manage.controller;

import com.dkd.common.core.domain.R;
import com.dkd.manage.pojo.dto.RegionQueryListDto;
import com.dkd.manage.pojo.entity.Region;
import com.dkd.manage.pojo.vo.RegionVo;
import com.dkd.manage.result.PageResult;
import com.dkd.manage.service.RegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "区域管理相关接口")
@RequestMapping("/manage/region")
public class RegionController {

    @Autowired
    private RegionService regionService;

    @ApiOperation("分页查询区域列表")
    @PreAuthorize("@ss.hasPermi('manage:region:list')")
    @GetMapping("/list")
    public R<PageResult<RegionVo>> list(RegionQueryListDto regionQueryListDto) {
        log.info("分页查询区域列表：{}", regionQueryListDto);
        PageResult<RegionVo> result = regionService.list(regionQueryListDto);
        return R.ok(result);
    }

    @ApiOperation("新增区域")
    @PreAuthorize("@ss.hasPermi('manage:region:add')")
    @PostMapping
    public R<Integer> add(@RequestBody Region region) {
        log.info("新增区域：{}", region);
        Integer row = regionService.add(region);
        return R.ok(row);
    }

    @ApiOperation("根据id查询区域")
    @PreAuthorize("@ss.hasPermi('manage:region:query')")
    @GetMapping("/{id}")
    public R<Region> getRegionById(@PathVariable Long id) {
        log.info("根据id查询区域：{}", id);
        Region region = regionService.getRegionById(id);
        return R.ok(region);
    }

    @ApiOperation("修改区域")
    @PreAuthorize("@ss.hasPermi('manage:region:edit')")
    @PutMapping
    public R<Integer> edit(@RequestBody Region region) {
        log.info("修改区域：{}", region);
        Integer row = regionService.edit(region);
        return R.ok(row);
    }

    @ApiOperation("根据id删除区域")
    @PreAuthorize("@ss.hasPermi('manage:region:remove')")
    @DeleteMapping("/{ids}")
    public R<Integer> deleteRegionById(@PathVariable Long[] ids) {
        log.info("根据id删除区域：{}", ids);
        Integer rows = regionService.deleteRegionById(ids);
        return R.ok(rows);
    }

}
